<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreBespeakCate;
use App\Http\Requests\UpdateBespeakCate;
use App\Models\Bespeak;
use App\Models\BespeakCate;
use App\Models\Fixfxs;
use App\Models\Havebespeak;
use App\Models\Store;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BespeakCateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */


    public function __construct()
    {
        view()->share([
            '_yyx' => 'am-in',
            '_bespeakcate' => 'am-active',
        ]);

    }
     //私教项目首页
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {
            //按挑战名称
            if($request->has('name')and $request->name !=''){
                $search = "%".$request->name."%";
                $query->where('name','like',$search);
            }
            //按门店
            if($request->has('store_id')and $request->store_id !='-1'){

                $query->where('store_id',$request->store_id);
            }
        };
        $cs=Store::all();
        $cates = BespeakCate::where($where)->with('store')->with('fixfxs')->orderBy('sort')->paginate(15);
        return view('admin.bespeakCate.index', compact('cates','cs'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //新增私教项目页面
    public function create()
    {
        $tags = Bespeak::orderBy('sort')->get();
        $stores=Store::all();
        $fixfxses=Store::with('fixfxses')->get();

        return view('admin.bespeakCate.create', compact('tags','stores','fixfxses'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    //执行新增
    public function store(StoreBespeakCate $request)
    {
        //除了bespeak_id字段其他全部接受
        $data = $request->except('bespeak_id');
        $bespeakcate = BespeakCate::create($data);
        $bespeak_id = $request->bespeak_id;
        foreach ($bespeak_id as $k => $b) {
            $info['bespeak_id'] = $b;
            $info['bcate_id'] = $bespeakcate->id;
            Havebespeak::create($info);
        }
        return redirect('/admin/bespeakCate')->with('success', '新增成功~');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    //编辑页面
    public function edit($id)
    {
        //全部标签
        $tags = Bespeak::orderBy('sort')->get();
        //全部门店
        $stores=Store::all();

        $fixfxses=Store::with('fixfxses')->get();
        //当前私教项目
        $bespeakCate = BespeakCate::find($id);


        //当前私教项目包含的标签ID，集合转化成数据
        $tag_id = Havebespeak::where('bcate_id', $id)->pluck('bespeak_id')->toArray();

        return view('admin.bespeakCate.edit', compact('bespeakCate','tag_id','tags','stores','fixfxses'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    //执行编辑
    public function update(UpdateBespeakCate $request, $id)
    {
        //当前私教
        $cate = BespeakCate::find($id);
        //除了bespeak_id字段其他全部接受
        $data=$request->except('bespeak_id');
        $cate->update($data);
        //关联私教规格更新
        $cate->bespeaks()->sync($request->bespeak_id);
        return redirect('/admin/bespeakCate')->with('success', '修改成功~');
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    //首先判断该分类下时候有项目
    public function destroy($id)
    {
        BespeakCate::destroy($id);
        Havebespeak::where('bcate_id',$id)->delete();

        return back()->with('success', '删除成功');
    }
     //Ajax排序
    public function sort(Request $request){
        $bespeakcate = BespeakCate::find($request->id);
        $bespeakcate->sort = $request->sort;
        $bespeakcate->save();
    }



}
